<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';

import type { VxeGridProps } from '#/adapter/vxe-table';
import type { ProductList } from '#/api/iot/product/model';

import { useRouter } from 'vue-router';

import { Page, useVbenDrawer } from '@vben/common-ui';
import { getVxePopupContainer } from '@vben/utils';

import { message, Popconfirm, Space } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getProductList, removeProduct } from '#/api/iot/product/index';
import { $t } from '#/locales';

import { columns, querySchema } from './data';
import productModal from './product-drawer.vue';

const router = useRouter();
const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: querySchema(),

  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
    // 点击行选中
    // trigger: 'row',
  },
  columns,
  height: 'auto',
  keepSource: true,

  toolbarConfig: {
    custom: false,
  },
  proxyConfig: {
    autoLoad: true,
    ajax: {
      query: async ({ page }, formValues = {}) => {
        const data = await getProductList({
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
        return {
          rows: data.rows,
          total: data.total,
        };
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  id: 'iot-product-index',
};

const [BasicTable, tableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});

const [ProductModal, drawerApi] = useVbenDrawer({
  connectedComponent: productModal,
});

function handleAdd() {
  drawerApi.setData({});
  drawerApi.open();
}

async function handleEdit(record: ProductList) {
  drawerApi.setData({ id: record.id });
  drawerApi.open();
}

async function handleDelete(row: ProductList) {
  await removeProduct([row.id]);
  await tableApi.query();
  message.success({
    content: $t('ui.actionMessage.deleteSuccess'),
    key: 'action_process_msg',
  });
}

function handleInfo(id: string) {
  router.push(`/product/detail/${id}`);
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="产品列表">
      <template #toolbar-tools>
        <Space>
          <a-button
            type="primary"
            v-access:code="['iot:product:add']"
            @click="handleAdd"
          >
            {{ $t('pages.common.add') }}
          </a-button>
        </Space>
      </template>
      <template #action="{ row }">
        <Space>
          <ghost-button
            v-access:code="['iot:product:query']"
            @click="handleInfo(row.id)"
          >
            详情
          </ghost-button>
          <ghost-button
            v-access:code="['iot:product:add']"
            @click="handleEdit(row)"
          >
            {{ $t('pages.common.edit') }}
          </ghost-button>

          <Popconfirm
            :get-popup-container="getVxePopupContainer"
            placement="left"
            title="确认删除？"
            @confirm="handleDelete(row)"
          >
            <ghost-button
              danger
              v-access:code="['iot:product:remove']"
              @click.stop=""
            >
              {{ $t('pages.common.delete') }}
            </ghost-button>
          </Popconfirm>
        </Space>
      </template>
    </BasicTable>
    <ProductModal @reload="tableApi.query()" />
  </Page>
</template>
